package cn.shyouchi.wuguanjia.android.api.youchi;

import com.alibaba.fastjson.JSON;

import java.io.File;

import cn.shyouchi.wuguanjia.android.api.ClientProvider;
import cn.shyouchi.wuguanjia.android.model.LoginMod;
import cn.shyouchi.wuguanjia.android.model.RegisterMod;
import cn.shyouchi.wuguanjia.android.model.SmsCodeMod;
import cn.shyouchi.wuguanjia.android.model.SmsVerifyMod;
import cn.shyouchi.wuguanjia.android.model.User;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import rx.Observable;

/**
 * Created by Eenie on 2016/10/12 at 14:32
 * Des:
 */

public class YouChiApi {

    private YouChiService mService;


    public YouChiApi() {
        Retrofit retrofit = ClientProvider.getDefaultClient().getRetrofit();
        mService = retrofit.create(YouChiService.class);
    }


    public Observable<SmsCodeMod> fetchSmsCode(String phone) {
        return mService.fetchSmsCode(phone);
    }


    public Observable<RegisterMod> register(User user) {
        return register(user.getUsername(),
                user.getPassword(),
                user.getName(),
                user.getGender(),
                user.getBirthday(),
                user.getAddress(),
                user.getNumber(),
                user.getAuthority(),
                user.getValidate(),
                user.getHeight(),
                user.getGraduate(),
                user.getName(),
                user.getLivingAddress(),
                JSON.toJSONString(user.getEmergencyContact()),
                user.getIndustry(),
                user.getSkill(),
                user.getChannel(),
                new File(user.getIdCardPositiveImagePath()),
                new File(user.getIdCardNegativeImagePath()),
                new File(user.getIdCardHeadImagePath()));
    }


    public Observable<RegisterMod> register(String username,
                                            String password,
                                            String name,
                                            String gender,
                                            String birthday,
                                            String address,
                                            String number,
                                            String publisher,
                                            String validate,
                                            int height,
                                            String graduate,
                                            String telephone,
                                            String livingAddress,
                                            String emergecyContact,
                                            String industry,
                                            String skill,
                                            String channel,
                                            File frontImg,
                                            File negativeImg,
                                            File headImg) {

        RequestBody usernameBody = RequestBody.create(MediaType.parse("multipart/form-data"), username);
        RequestBody passwordBody = RequestBody.create(MediaType.parse("multipart/form-data"), password);
        RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), name);
        RequestBody genderBody = RequestBody.create(MediaType.parse("multipart/form-data"), gender);
        RequestBody birthdayBody = RequestBody.create(MediaType.parse("multipart/form-data"), birthday);
        RequestBody addressBody = RequestBody.create(MediaType.parse("multipart/form-data"), address);
        RequestBody numberBody = RequestBody.create(MediaType.parse("multipart/form-data"), number);
        RequestBody publisherBody = RequestBody.create(MediaType.parse("multipart/form-data"), publisher);
        RequestBody validateBody = RequestBody.create(MediaType.parse("multipart/form-data"), validate);
        RequestBody heightBody = RequestBody.create(MediaType.parse("multipart/form-data"), height + "");
        RequestBody graduateBody = RequestBody.create(MediaType.parse("multipart/form-data"), graduate);
        RequestBody telephoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), telephone);
        RequestBody livingAddressBody = RequestBody.create(MediaType.parse("multipart/form-data"), livingAddress);
        RequestBody emergencyBody = RequestBody.create(MediaType.parse("multipart/form-data"), emergecyContact);
        RequestBody industryBody = RequestBody.create(MediaType.parse("multipart/form-data"), industry);
        RequestBody skillBody = RequestBody.create(MediaType.parse("multipart/form-data"), skill);
        RequestBody channelBody = RequestBody.create(MediaType.parse("multipart/form-data"), channel);

        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), frontImg);
        // MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body = MultipartBody.Part.createFormData("id_card_positive", frontImg.getName(), requestFile);
        RequestBody negativeRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), negativeImg);
        MultipartBody.Part negativeBody = MultipartBody.Part.createFormData("id_card_negative", negativeImg.getName(), negativeRequestBody);
        RequestBody headRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), headImg);
        MultipartBody.Part headBody = MultipartBody.Part.createFormData("id_card_head_image", headImg.getName(), headRequestBody);

        return mService.register(usernameBody,
                passwordBody,
                nameBody,
                genderBody,
                birthdayBody,
                addressBody,
                numberBody,
                publisherBody,
                validateBody,
                heightBody,
                graduateBody,
                telephoneBody,
                livingAddressBody,
                emergencyBody,
                industryBody,
                skillBody,
                channelBody,
                body,
                negativeBody,
                headBody);
    }


    public Observable<SmsVerifyMod> verifySmsCode(String token, String code) {
        return mService.smsVerify(token, code);
    }


    public Observable<LoginMod> login(String name, String pwd) {
        return mService.login(name, pwd);
    }


}
